package com.wd.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.wd.common.base.BaseFragment
import com.wd.home.adapter.HomeFindAdapter
import com.wd.home.adapter.HomeManylistAdapter
import com.wd.home.databinding.FragmentHomeBinding

class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    val viewmodel: HomeViewmodel by viewModels()
    private var homefindadapter: HomeFindAdapter? = null
    private var homemaanyadapter: HomeManylistAdapter? = null

    override fun initData() {

        //轮播图
        viewmodel.hbanner()
        viewmodel.homebanner.observe(this, Observer {
            binding.homeBanner.setData(it!!, null)
            binding.homeBanner.setPageChangeDuration(3000)
            binding.homeBanner.setmAdapter { banner, model, view, position ->
                Glide.with(this)
                    .load(it.get(position).imageUrl)
                    .into(view as ImageView)
            }
        })
        //首页金刚区


        val arr1: Array<Int> = arrayOf(
            R.mipmap.home_nav_ranking,
            R.mipmap.home_nav_circles,
            R.mipmap.home_nav_look,
            R.mipmap.home_nav_new,
            R.mipmap.home_nav_newest,
            R.mipmap.home_nav_look,
            R.mipmap.home_nav_valuation,
            R.mipmap.home_nav_fuel,
            R.mipmap.home_nav_circles,
            R.mipmap.home_nav_electric)
        viewmodel.homefindind()

        viewmodel.homefindLiveData.observe(this, Observer {

            homefindadapter = context?.let { it1 -> HomeFindAdapter(it1, it.result,arr1) }

            binding.homefindRecyId.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            binding.homefindRecyId.adapter = homefindadapter

        })
        var page = 1
        //上拉刷新 下拉加载
        viewmodel.homemanylist(page, 10)
        viewmodel.homeManyListLiveData.observe(this, {
            homemaanyadapter = context?.let { it1 -> HomeManylistAdapter(it1, it) }
            binding.manyrec.layoutManager = LinearLayoutManager(context)
            binding.manyrec.adapter = homemaanyadapter
        })

        val refreshLayout = binding.refreshLayout as RefreshLayout
        refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewmodel.homemanylist(page, 10)
                refreshLayout.finishRefresh(2000 /*,false*/)
            }
        })


        refreshLayout.setOnLoadMoreListener { refreshLayout ->
            page++
            viewmodel.homemanylist(page, 10)
            refreshLayout.finishLoadMore(2000 /*,false*/)
        }


    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeBinding = FragmentHomeBinding.inflate(layoutInflater)

}